import type { ElementHiderRuleConfig } from '../../../types/constants'
import { PageHandler } from '../../../lib/page_handler'
import { GreasyForkPageType } from '../../../lib/page_handler/platform/greasyfork/types'
import { ToolKit } from '../../../lib/utils/toolkit'
import { ElementSelectorDOMScopeType, ElementSelectorPosition, ElementSelectorType } from '../../../types/constants'
import { ELEMENT_IDENTIFIER_CONFIG } from '../../service/element_identifier/types'

export const GREASY_FORK_WEB_ELEMENT_CONFIG: ElementHiderRuleConfig = {
  [ELEMENT_IDENTIFIER_CONFIG.base.name.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#script-info header h2',
    nameZh: '脚本名称',
    nameEn: 'scriptName',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.description.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#script-info #script-description',
    nameZh: '脚本描述',
    nameEn: 'scriptDescription',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.pagination.prevPage.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#script-info #script-content .discussion-up-level',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.feedbackType.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '#script-info .discussion-header h2',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.subject.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '.text-content .discussion-header h2',
    nameZh: '讨论主题',
    nameEn: 'discussionSubject',
    position: ElementSelectorPosition.TopCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.DISCUSSIONS_DETAIL,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.ad.googleAd.id]: {
    hide: true,
    selectorType: ElementSelectorType.Class,
    selector: '.adsbygoogle',
    position: ElementSelectorPosition.MiddleLeft,
    listen: true,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.INDEX,
        GreasyForkPageType.SCRIPT_INFO,
        GreasyForkPageType.SCRIPT_VERSIONS,
        GreasyForkPageType.SCRIPT_FEEDBACK,
      ]
      return !ToolKit.browser.platform.isMobileMode() && supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.publishTime.id]: {
    hide: false,
    selectorType: ElementSelectorType.JQuery,
    selector: '.comment-meta .comment-meta-item:contains(\'发布于\'), .discussion-meta .discussion-meta-item:has(> .script-link) + .discussion-meta-item relative-time',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
        GreasyForkPageType.DISCUSSIONS_DETAIL,
        GreasyForkPageType.DISCUSSIONS,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.latestMessage.id]: {
    hide: false,
    selectorType: ElementSelectorType.Mix,
    selector: '.discussion-meta .discussion-meta-item:last-child',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.DISCUSSIONS,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.editTime.id]: {
    hide: false,
    selectorType: ElementSelectorType.JQuery,
    selector: '.comment-meta .comment-meta-item:contains(\'编辑于\')',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
        GreasyForkPageType.DISCUSSIONS_DETAIL,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.quote.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '.comment-meta .comment-meta-item:has(> .quote-comment)',
    nameZh: '引用留言',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
        GreasyForkPageType.DISCUSSIONS_DETAIL,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.report.id]: {
    hide: false,
    selectorType: ElementSelectorType.Complex,
    selector: '.comment-meta .comment-meta-item:has(> .report-link)',
    nameZh: '举报留言',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
        GreasyForkPageType.DISCUSSIONS_DETAIL,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.base.publish.id]: {
    hide: false,
    selectorType: ElementSelectorType.Id,
    selector: '#post-reply',
    nameZh: '发布留言',
    position: ElementSelectorPosition.MiddleCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_DISCUSSION,
        GreasyForkPageType.DISCUSSIONS_DETAIL,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.component.filterComponent.id]: {
    hide: false,
    selectorType: ElementSelectorType.Multiple,
    selector: '.sidebarred .sidebar:has(#script-list-option-groups, #user-list-option-groups)',
    position: ElementSelectorPosition.MiddleRight,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_LIST,
        GreasyForkPageType.USERS,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.privacyPreferenceHover.id]: {
    hide: false,
    domScope: ElementSelectorDOMScopeType.ShadowRoot,
    hostSelector: '.fc-ccpa-root + div',
    selectorType: ElementSelectorType.Multiple,
    selector: '#ft-floating-toolbar, .ipr-container',
    position: ElementSelectorPosition.BottomLeft,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.SCRIPT_INFO,
        GreasyForkPageType.SCRIPT_VERSIONS,
        GreasyForkPageType.SCRIPT_LIST,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.ad.googleAdTwo.id]: {
    hide: true,
    selectorType: ElementSelectorType.Id,
    selector: '#google_pedestal_container',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      return ToolKit.browser.platform.isMobileMode()
    },
  },
  [ELEMENT_IDENTIFIER_CONFIG.default.privacyPreference.id]: {
    hide: false,
    domScope: ElementSelectorDOMScopeType.ShadowRoot,
    hostSelector: 'div[style*=\'color-scheme: initial\']',
    selectorType: ElementSelectorType.Class,
    selector: '.ipr-container',
    position: ElementSelectorPosition.BottomCenter,
    enable: () => {
      const supportPages = [
        GreasyForkPageType.INDEX,
        GreasyForkPageType.SCRIPT_INFO,
        GreasyForkPageType.SCRIPT_FEEDBACK,
      ]
      return supportPages.includes(PageHandler.getCurrentPageType() as GreasyForkPageType)
    },
  },
}
